# -*- coding:utf-8 -*-
from typing import TypeVar, Generic
from pydantic import BaseModel
from fastapi import APIRouter, Depends, Request, Security, BackgroundTasks

from utils.response import ResponsePage, ResponseModel, Page
from utils.dependence import get_page_filter, PageFilter

from core.exceptions import ServiceException

from services.user import check_login_user
from services.permision import PermisionService
from schemas.permision import Permision, PermisionCreate, PermisionUpdate

router = APIRouter(prefix="/permision")

@router.get("", response_model=ResponsePage[Permision], summary=f"权限列表查询", dependencies=[Security(check_login_user, scopes=["admin:permision:access"])])
async def pages(page_no: int=1, page_size: int=10, page_filter: PageFilter = Depends(get_page_filter), service: PermisionService = Depends(PermisionService)):
    page = await service.pages(page_no, page_size, page_filter)
    return ResponsePage.successed(data=Page[Permision].from_orm(page))

@router.get("/{id}", response_model=ResponseModel[Permision], summary=f"权限id查询", dependencies=[Security(check_login_user, scopes=["admin:permision:access"])])
async def info_by_id(id: int, service: PermisionService = Depends(PermisionService)):
    data_db = await service.get_by_id(id)
    if not data_db:
        raise ServiceException(404, message="权限不存在")
    return ResponseModel.successed(data=Permision.from_orm(data_db))

@router.post("", response_model=ResponseModel[Permision], summary=f"权限创建", dependencies=[Security(check_login_user, scopes=["admin:permision:create"])])
async def do_create(request: Request, data: PermisionCreate, service: PermisionService = Depends(PermisionService)):
    data_db = await service.create(request, data)
    return ResponseModel.successed(message="权限信息新增成功", data=Permision.from_orm(data_db))

@router.delete("/{id}", response_model=ResponseModel[Permision], summary=f"权限删除", dependencies=[Security(check_login_user, scopes=["admin:permision:delete"])])
async def do_delete(request: Request, id: int, service: PermisionService = Depends(PermisionService)):
    data_db = await service.delete(request, id)
    return ResponseModel.successed(message="权限信息删除成功", data=Permision.from_orm(data_db))

@router.put("/{id}", response_model=ResponseModel[Permision], summary=f"权限更新", dependencies=[Security(check_login_user, scopes=["admin:permision:update"])])
async def do_update(request: Request, id: int, data: PermisionUpdate, service: PermisionService = Depends(PermisionService)):
    data_db = await service.update(request, id, data)
    return ResponseModel.successed(message="权限信息更新成功", data=Permision.from_orm(data_db))
